import { _decorator, Component, Label, Node } from 'cc';
import EventBus from './EventBus';
import { EventConst } from './EvenetConst';
const { ccclass, property } = _decorator;

@ccclass('UILayer')
export class UILayer extends Component {

    @property(Node)
    timeLabel: Node = null;

    @property(Node)
    bdBg: Node = null;

    @property(Node)
    tips: Node = null;

    @property(Label)
    levelLabel: Label = null;

    start() {
        this.schedule(this.mainTick, 1)
        EventBus.on(EventConst.EliminationEnd, this.onComplateClickEliminationBlock, this);
        EventBus.on(EventConst.EliminationEnd, this.onComplateClickEliminationBlockColor, this);
    }
    protected onDestroy(): void {
        EventBus.off(EventConst.EliminationEnd, this.onComplateClickEliminationBlock, this);
        EventBus.off(EventConst.EliminationEnd, this.onComplateClickEliminationBlockColor, this);
    }
    //监听移除色块完成事件
    onComplateClickEliminationBlock() {
        this.tips.active = false;
        this.tips.getChildByName('RemoveTips').active = false;
    }
    //监听多个色块颜色移除事件
    onComplateClickEliminationBlockColor() {
        this.tips.active = false;
        this.tips.getChildByName('MagnetTips').active = false;
    }
    //倒计时停止
    onTimeStop() {
        if (this.mainReingSeconds <= 0) {
            // 已经结束，保持停止
            this.mainIsRunning = false;
            this.unschedule(this.mainTick);
            return;
        }
        // 进入20秒冷却倒计时，然后继续主倒计时

        this.freeReingSeconds = 5;
        this.freezeIsRunning = true;
        // 暂停阶段保持显示不变，不再主动刷新标签
        // 确保调度有效
        this.unschedule(this.mainTick);
        this.schedule(this.freezeTick, 1);
        if (this.bdBg) this.bdBg.active = true;
    }


    //主倒计时
    mainReingSeconds: number = 180;
    mainIsRunning: boolean = true;
    mainTick() {
        if (!this.mainIsRunning) return;
        this.mainReingSeconds--;
        if (this.mainReingSeconds <= 0) {

            this.mainReingSeconds = 0;
            this.unschedule(this.mainTick);
            // 计时彻底结束，隐藏暂停背景
        }
        this.updateLabel();
    }
    //end

    //冻结倒计时
    freeReingSeconds: number = 5;
    freezeIsRunning: boolean = true;
    freezeTick() {
        console.log('freezeTick执行');
        if (!this.freezeIsRunning) {
            return;
        }
        this.freeReingSeconds--;
        if (this.freeReingSeconds <= 0) {
            this.bdBg.active = false;
            this.freeReingSeconds = 0;
            this.unschedule(this.freezeTick);
            this.schedule(this.mainTick, 1)
        }
    }
    //end

    //更新时间
    private updateLabel() {
        const minutes = Math.floor(this.mainReingSeconds / 60);
        const seconds = this.mainReingSeconds % 60;
        const labelCmp = this.timeLabel?.getComponent(Label);
        if (labelCmp) {
            labelCmp.string = `${this.pad(minutes)}:${this.pad(seconds)}`;
        }
    }

    private pad(n: number) {
        return n.toString().padStart(2, '0');
    }

    onRemove() {
        this.tips.active = true;
        this.tips.getChildByName('RemoveTips').active = true;
        EventBus.emit(EventConst.PrepareClickEliminationBlock);
    }
    onMagnet() {
        this.tips.active = true;
        this.tips.getChildByName('MagnetTips').active = true;
        EventBus.emit(EventConst.PrepareClickEliminationBlockColor);
    }

    update(deltaTime: number) {

    }
}


